package com.mingyizhudao.app.view;

import java.io.File;
import java.io.InputStream;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.util.Log;

import com.alipay.euler.andfix.patch.PatchManager;
import com.dixiang.framework.BaseApplication;
import com.igexin.sdk.PushManager;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.CommonUtils;
import com.umeng.socialize.PlatformConfig;

public class MYApplication extends BaseApplication{
	private final static String ACTION = "com.loacaldata.service";
	
	
	public static float density ,heightPixels , widthPixels;//依次屏幕密度， 屏幕高度，屏幕宽度
	public static int startheight; //状态栏高度
	@Override
	public void onCreate() {
		super.onCreate();
		init();
		initShare();
		initUpdatePatch();
	}
	
	private void init(){
		
		Intent intent = new Intent();
		intent.setAction(ACTION);
		intent.setPackage(getPackageName());
		startService(intent);
	}
	
	private void initShare(){
        PlatformConfig.setWeixin("wx5f0bf0186b056809", "307d4a79eba2d85d62e8e29438da95bc");
        PlatformConfig.setSinaWeibo("190174859", "8d3a10da547aafd63f6372a9a0a5301d");
        PlatformConfig.setQQZone("1104681417", "jwk9InoCMkjJGwSo");
	}
	
	
	private void initUpdatePatch(){
		try {
			PatchManager mPatchManager = new PatchManager(this);
	        PackageManager packageManager = this.getPackageManager();
	        PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(),PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
	        mPatchManager.init(packageInfo.versionName);
	        File file=new File(getApplicationContext().getFilesDir().getPath()+"/apatch/apppatch.apatch");
            if(file.exists()){
                file.delete();
            }
	        mPatchManager.loadPatch();
	        
	        
	        
 	        String currentCode = ""+CommonUtils.getAppVersionCode(getApplicationContext());
	        if (currentCode.equals( CommonUtils.getFailApatchCode(getApplicationContext()))) {
	        	Intent intentService = new Intent();
				intentService.putExtra("download_url", CommonUtils.getFailApatchUrl(getApplicationContext()));
				intentService.setAction("com.updateapk.service");
				intentService.setPackage(getApplicationContext().getPackageName());
				getApplicationContext().startService(intentService);
	        	
			}else {
				String myPatch = getApplicationContext().getFilesDir().getPath()+"/myzd/apppatch.apatch";
		        File myPatchFile=new File(getApplicationContext().getFilesDir().getPath()+"/myzd/apppatch.apatch");
		        if (myPatchFile.exists()) {
		        	
			        if (currentCode.equals( CommonUtils.getApatchCode(getApplicationContext()))) {
			        	mPatchManager.addPatch(myPatch);
					}else {
						
						myPatchFile.delete();
						CommonUtils.setApatchCode(getApplicationContext(), "");
					}
				}
			}
	        
	        
	        
	        
//	        String patchFileString = Environment.getExternalStorageDirectory().getAbsolutePath() + APATCH_PATH;
//			mPatchManager.addPatch(patchFileString);
	        
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
